package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

@WebServlet("/demo3")
public class demo3 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据名称获取数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        System.out.println("----------");

        //根据名称获取所有数据
        String[] hobbies = req.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        System.out.println("------------");

        //获取所有名称
        Enumeration<String> parameterNames = req.getParameterNames();

        while (parameterNames.hasMoreElements()){
            String names =parameterNames.nextElement();
            System.out.println(names);
        }
        System.out.println("------------");
        //获取所有参数键值对
        Map<String, String[]> parameterMap = req.getParameterMap();
        for (String key: parameterMap.keySet()) {
            String[] values = parameterMap.get(key);
            System.out.println(key+":");
            for (String value : values) {
                System.out.println(value);
            }
            System.out.println();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
